package com.yanqu.road.server.gameplayer.module.sign;

import com.yanqu.road.entity.log.LogOnlineTime;
import com.yanqu.road.logic.bussiness.player.UserSignBussiness;
import com.yanqu.road.logic.pb.SignPb;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserSign;
import com.yanqu.road.pb.sign.SignProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;

import java.util.Date;

public class SignModule extends GeneralModule {

    private UserSign userSign;

    public SignModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userSign = UserSignBussiness.getUserSign(player.getUserId());
        initUserSign();
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (null != userSign) {
            if (userSign.isInsertOption()) {
                UserSignBussiness.addUserSign(userSign);
            } else if (userSign.isUpdateOption()) {
                UserSignBussiness.updateUserSign(userSign);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        syncUserSign();
    }

    @Override
    public void loginSendMsg() {

    }

    private void initUserSign() {
        if (null == userSign) {
            UserSign signData = new UserSign();
            signData.setUserId(player.getUserId());
            signData.setSignTime(0);
            signData.setSignDays(0);
            signData.setTotalSignDays(0);
            signData.setInsertOption();
            userSign = signData;
        }
    }

    /**
     * 同步签到数据
     */
    public void syncUserSign() {
        SignProto.SignDataMsg.Builder msg = SignPb.parseSignDataMsg(userSign, ConfigMgr.getSignRewardMap().size());
        if(null != msg) {
            player.sendPacket(Protocol.U_SIGN_DATA, msg);
        }
    }

    public int userSign() {
        initUserSign();
        if (DateHelper.isToday(new Date(userSign.getSignTime() * 1000))) {
            return GameErrorCode.E_ALREADY_SIGN;
        }
        //更新签到数据
        userSign.setSignTime(System.currentTimeMillis() / 1000);
        userSign.setSignDays(userSign.getSignDays() + 1);
        if(userSign.getSignDays() > ConfigMgr.getSignRewardMap().size()){
            userSign.setSignDays(userSign.getSignDays() % ConfigMgr.getSignRewardMap().size());
        }
        userSign.setTotalSignDays(userSign.getTotalSignDays() + 1);
        //增加签到奖励
        Property property = ConfigMgr.getSignProperty(userSign.getSignDays());
        if (null != property) {
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Sign, eLogMoneyType.SignDailyGetReward);
        }

        return 0;
    }

    public UserSign getUserSign() {
        return userSign;
    }

    /**
    * 描述：记录在线时长
    * 作者：zrq
    * 时间：2022/8/20
    * 参数：

    * 返回值：
    **/
    public void logOnlineTime(long onlineTimeMark, long onlineTime){
        if(null != userSign){
            synchronized (userSign){
                if(userSign.getOnlineTimeMark() != onlineTimeMark){
                    userSign.setOnlineTime(onlineTime);
                    userSign.setOnlineTimeMark(onlineTimeMark);
                }else {
                    userSign.setOnlineTime(userSign.getOnlineTime() + onlineTime);
                }
                AutoLogMgr.add(new LogOnlineTime(player.getUserId(), userSign.getOnlineTime(), userSign.getOnlineTimeMark()));
            }
        }
    }

    /**
     * 获取今日在线时间（不是很准，只能知道个大概）
     */
    public long getTodayOnlineTime() {
        long now = System.currentTimeMillis();
        long loginTime = player.getUserInfo().getLastLoginTime() * 1000;
        long zeroTime = DateHelper.getZeroTimeStamp(now);
        if (loginTime < zeroTime) {
            return now - zeroTime; // 过0点
        }

        // 这个数据不重要，本来也不精确，就这样避免并发了
        if (Math.abs(now - zeroTime) < DateHelper.SECOND_MILLIONS * 30) {
            return 0;
        }

        long onlineTime = userSign.getOnlineTime();
        long loginStartTime = player.getLoginStartTime();
        return now - loginStartTime + onlineTime;
    }

}
